Page({
  data: {
    marketList: [],
    addressList: [],
    addressId: '',
    isShow: false
  },
  onLoad: function (options){
    var scan_url = decodeURIComponent(options.q);
    var exFrom = this.getUrlData(scan_url.split("?")[1], "from")
    console.log(exFrom)
    this.setData({
      exFrom: exFrom
    })
  },
  onShow: function () {
    this.setData({
      isShow: false
    })
    var srcpage = "index";
    getApp().rueLogin(this.getAddressList, this, srcpage);
  },
  //收货地址相关
  getAddressList() {
    var url = getApp().data.url + 'userexpand/getAddress';
    var body = {};
    let showFlage = this.data.exFrom
    if (showFlage == 'exhibition'){
      wx.setStorageSync('supermarket_id', 'S101')
      wx.setStorageSync('seller_id', 'C101')
      wx.setStorageSync('supername', '如e测试店')
      wx.setStorageSync('superaddress', '北京市丰台区南三环西路16号搜宝商务中心3号楼1107')
      wx.removeStorageSync('selectLoction');
      wx.removeStorageSync('indexObj');
      wx.removeStorageSync('fromFlage')
      wx.reLaunch({
        url: '../eIndex/eIndex'
      })
    }else{
      getApp().wxAjax(this.spliceAddress, url, body);
    }
    
  },
  spliceAddress(res) {
    var resObj = res.data.body;
    console.log(resObj)
    var addressList;
    if (typeof resObj.address_list == "undefined") {
      addressList = 0;
    } else {
      var sLocation = wx.getStorageSync('selectLoction')
      if (sLocation == "") {
        addressList = resObj.address_list[0];
      } else {
        addressList = sLocation;
      }

    }
    console.log(addressList)
    var addressId = addressList.address_id;
    if (typeof addressId == 'undefined') {
      addressId: ''
    }
    this.setData({
      addressList: addressList,
      addressId: addressList.address_id
    })
    this.getAddressTaW(addressList);
  },
  //超市列表相关
  getAddressTaW(addressObj) {
    var me = this;
    var tawData = {};
    if (addressObj == 0) {
      wx.getLocation({
        type: 'gcj02',
        success: function (res) {
          tawData.through = res.longitude;
          tawData.weft = res.latitude;
          me.getStoreListinfo(tawData);
        }
      })
    } else {
      tawData.through = addressObj.through;
      tawData.weft = addressObj.weft;
      me.getStoreListinfo(tawData);
    }

  },
  getStoreListinfo(tawData) {
    var url = getApp().data.url + 'sysinfo/getSupermarketInfoByLAL';
    var body = tawData;
    getApp().wxAjax(this.spliceSupermareke, url, body);

  },
  spliceSupermareke: function (res) {
    var resObj = res.data;
    console.log(resObj.supermarkets)
    if (resObj.header.res_code == 0) {
      this.setData({
        marketList: resObj.body.supermarkets
      })
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
    this.setData({
      isShow: true
    })
  },
  toaddressList() {
    wx.navigateTo({
      url: '../addressList/addressList?form=index',
    })
  },
  toeindex(e) {
    var marketList = this.data.marketList;
    var index = e.currentTarget.dataset.index;
    var supermarket_id;
    var seller_id;
    var supername;
    var superaddress;
    for (let i = 0; i < marketList.length; i++) {
      if (index == i) {
        supermarket_id = marketList[i].supermarket_id;
        seller_id = marketList[i].seller_id;
        supername = marketList[i].supermarket_name;
        superaddress = marketList[i].supermarket_address;
        break;
      }
    }
    wx.setStorageSync('supermarket_id', supermarket_id);
    wx.setStorageSync('seller_id', seller_id);
    wx.setStorageSync('supername', supername);
    wx.setStorageSync('superaddress', superaddress);
    wx.setStorageSync('addressId', this.data.addressId)
    wx.removeStorageSync('selectLoction');
    wx.removeStorageSync('indexObj');
    wx.removeStorageSync('fromFlage');
    wx.reLaunch({
      url: '../eIndex/eIndex'
    })
  },
  toAddAddress() {
    wx.setStorageSync('fromFlage', 'index')
    wx.navigateTo({
      url: '../addAddress/addAddress?pageFrom=index'
    })
  },
  getUrlData: function (url, name) {
    url = "?" + url;
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = url.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
  },
})